// src/main/java/com/xiran/todoself/common/Response.java
package com.xiran.todoself.common;

import com.sun.org.apache.xpath.internal.objects.XNull;
import lombok.Data;

import java.io.Serializable;

/**
 * 统一响应结果封装类
 * @param <T> 响应数据类型
 */
@Data
public class Response<T> implements Serializable {

    private static final long serialVersionUID = 1L;

    public static final int CODE_SUCCESS = 200;
    public static final int CODE_FAIL = 500;
    public static final int CODE_UNAUTHORIZED = 401;
    public static final int CODE_NOT_FOUND = 404;

    private int code;
    private String message;
    private T data;

    // 私有构造函数
    private Response() {}

    private Response(int code, String message, T data) {
        this.code = code;
        this.message = message;
        this.data = data;
    }

    public Response(Boolean code, String message) {
        this.code = code ? 1 : 0;
        this.message = message;
    }

    public Response(String message) {
        this.message = message;
    }

    // ✅ 成功响应
    public static <T> Response<T> success() {
        return success(null);
    }

    public static <T> Response<T> success(T data) {
        return new Response<>(CODE_SUCCESS, "success", data);
    }

    public static <T> Response<T> success(int code, String message) {
        Response<T> response = new Response<>();
        response.setCode(code);
        response.setMessage(message);

        return response;
    }

    // ✅ 失败响应
    public static <T> Response<T> fail(int code, String message) {
        return new Response<>(code, message, null);
    }

    // ✅ 失败响应
    public static <T> Response<T> fail(Boolean code, String message) {

        return new Response<>(code,message);
    }

    public static <T> Response<T> fail(String message) {
        return  fail(CODE_FAIL, message);
    }



    // ✅ 快捷失败方法
    public static <T> Response<T> unauthorized(String message) {
        return fail(CODE_UNAUTHORIZED, message);
    }

    public static <T> Response<T> notFound(String message) {
        return fail(CODE_NOT_FOUND, message);
    }


    // ✅ 链式调用（可选）
    public Response<T> code(int code) {
        this.code = code;
        return this;
    }

    public Response<T> message(String message) {
        this.message = message;
        return this;
    }

    public Response<T> data(T data) {
        this.data = data;
        return this;
    }
}